package com.ht.shop.utils;

import com.ht.shop.pojo.TbUser;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;

public class MailUtils {

    public static void sendMail(TbUser user) throws MessagingException {
        Properties properties = new Properties();
        properties.setProperty("mail.host","smtp.qq.com");
        properties.setProperty("mail.transport.protocol","smtp");
        properties.setProperty("mail.smtp.auth","true");
        Session session = Session.getDefaultInstance(properties);
        session.setDebug(true);

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("2719499081@qq.com"));
        message.setSubject("欢迎注册相亲网");//邮件主题
        message.addRecipient(Message.RecipientType.TO,new InternetAddress(user.getEmail()));
        message.setContent("欢迎进入相亲照骗网，本网站本着照片不骗，不是人的原则，专业行骗20年，感谢你步入被骗的路途" +
                "点击下面链接让我们骗你 http://localhost:8080/active?username="+user.getUsername()+"&code="+
                user.getCode(),"text/html;charset=UTF-8");
        message.setSentDate(new Date());//立即发送
        message.saveChanges();

        Transport transport = session.getTransport();
        transport.connect("2719499081@qq.com","qfnlwgftkraddhdb");
        transport.sendMessage(message,message.getAllRecipients());//发送
        transport.close();
    }
}
